﻿var radius = 6378137.0;//地球半径
var radiiii = 6378245.0;//非地球半径，经纬度转换用
var ee = 0.00669342162296594323;
var pi = Math.PI;

/// <summary>
/// WGS84坐标转百度坐标：换算算法
/// </summary>
/// <param name="lon"></param>
/// <param name="lat"></param>
function WGS84ToBD09(lon,lat)
{
    var coord=WGS84ToGCJ02(lon, lat);

    coord =GCJ02ToBD09(coord.lon, coord.lat);
    return coord;
}

/// <summary>
/// GPS坐标转火星坐标：换算算法
/// </summary>
/// <param name="lon"></param>
/// <param name="lat"></param>
function WGS84ToGCJ02(lon, lat)
{
    var dLat = transformLat(lon - 105.0, lat - 35.0);
    var dLon = transformLon(lon - 105.0, lat - 35.0);
    var radLat = lat / 180.0 * pi;
    var magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    var sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / ((radiii * (1 - ee)) / (magic * sqrtMagic) * pi);
    dLon = (dLon * 180.0) / (radiii / sqrtMagic * Math.cos(radLat) * pi);
    lat = lat + dLat;
    lon = lon + dLon;
    return {lon:lon,lat:lat};
}

/// <summary>
/// 火星坐标系 (GCJ-02) To 百度坐标系 (BD-09) 的转换算法（可用于谷歌转百度坐标）
/// </summary>
/// <param name="lon">返回百度lon</param>
/// <param name="lat">返回百度lat</param>
function GCJ02ToBD09(lon, lat)
{
    var x_pi = pi * 3000.0 / 180.0;
    var x = lon, y = lat;
    var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
    lon = z * Math.cos(theta) + 0.0065;
    lat = z * Math.sin(theta) + 0.006;
    return {lon:lon,lat:lat};
}

/// <summary>
/// 百度坐标系 (BD-09) To 火星坐标系 (GCJ-02) 的转换算法（可用于百度转谷歌坐标）
/// </summary>
/// <param name="lon"></param>
/// <param name="lat"></param>
function BD09ToGCJ02(lon,lat)
{
    var x_pi = pi * 3000.0 / 180.0;
    var x = lon - 0.0065, y = bd_lat - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    lon = z * Math.cos(theta);
    lat = z * Math.sin(theta);
    return {lon:lon,lat:lat};
}

/// <summary>
/// 此方法是近似值，且是违法的，不要声张
/// </summary>
function GCJ02ToWGS84(lon,lat)
{
    var dLat = transformLat(lon - 105.0, lat - 35.0);
    var dLon = transformLon(lon - 105.0, lat - 35.0);
    var radLat = lat / 180.0 * pi;
    var magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    var sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / ((radiii * (1 - ee)) / (magic * sqrtMagic) * pi);
    dLon = (dLon * 180.0) / (radiii / sqrtMagic * Math.cos(radLat) * pi);

    lon = lon - dLon;
    lat = lat - dLat;
    return {lon:lon,lat:lat};
}

/// <summary>
/// GPS转火星经度转换过程算法
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
function transformLon( x,  y)
{
    var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
    return ret;
}

/// <summary>
/// GPS转火星纬度转换过程算法
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
function transformLat( x,  y)
{
    var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
    return ret;
}

/// <summary>
/// WGS84经纬度转Web Mercator
/// </summary>
/// <param name="lon"></param>
/// <param name="lat"></param>
/// <returns></returns>
function LonLat2Mercator(lon, lat)
{
    var x = lon * 20037508.342789 / 180;
    var y = Math.log(Math.tan((90 + lat) * pi / 360)) / (pi / 180);
    y = y * 20037508.34789 / 180;
    return [ x, y];
}

/// <summary>
/// web Mercator转WGS84经纬度
/// </summary>
/// <param name="mercatorX"></param>
/// <param name="mercatorY"></param>
/// <returns></returns>
function Mercator2LonLat( mercatorX,  mercatorY)
{
    var x = mercatorX / 20037508.34 * 180;
    var y = mercatorY / 20037508.34 * 180;
    y = 180 / pi * (2 * Math.atan(Math.exp(y * pi / 180)) - pi / 2);

    return { lon: x, lat: y };
}
